package gold.digger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC1004 {
    public long startExecuteTime = System.currentTimeMillis();

    /*
     * @param 此题目直接用别人代码，自己只理解思想
     * 经典的滑动窗口解法
     * @return:
     */
    class Solution {
        public int longestOnes(int[] A, int K) {
            int count = 0;//统计0的个数
            int left = 0;//滑动窗口区间左端点
            int right = 0;//滑动窗口区间右端点
            int res = 0;//最终结果
            //滑动窗口表示的区间为[left,right)，左闭右开
            while (right < A.length) {
                if (A[right++] == 0) {//窗口扩充一个元素，如果为0则count++；
                    count++;
                }
                while (count > K) {//当窗口内0的个数超过k时候，开始收缩窗口
                    if (A[left++] == 0) {//如果刚滑出窗口的元素是0，则count--;
                        count--;
                    }
                }
                //此时count<=K,保存窗口的最大宽度
                res = Math.max(res, right - left);
            }
            return res;
        }
    }


    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC1004 an = new LC1004();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
